// 文件名称: RandomGroupSelectionController.java
// 创建日期: 2023-10-05
// 描述: 随机分组选择控制器类，负责处理随机分组选择界面的各种操作。

package org.wisdom.studentchoose;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import org.wisdom.studentchoose.db.Student;
import org.wisdom.studentchoose.db.StudentService;

import java.io.IOException;

public class RandomGroupSelectionController {

    @FXML
    private TableView<Student> studentTable; // 学生列表表格

    @FXML
    private TableColumn<Student, String> nameColumn; // 学生姓名列

    @FXML
    private TableColumn<Student, String> numberColumn; // 学生学号列

    @FXML
    private TableColumn<Student, String> classNameColumn; // 学生班级列

    @FXML
    private TableColumn<Student, String> groupNameColumn; // 学生小组列

    @FXML
    private Label selectedGroupLabel; // 选择的小组标签

    @FXML
    public void initialize() {
        // 初始化表格列的 cellValueFactory
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        numberColumn.setCellValueFactory(new PropertyValueFactory<>("number"));
        classNameColumn.setCellValueFactory(new PropertyValueFactory<>("className"));
        groupNameColumn.setCellValueFactory(new PropertyValueFactory<>("groupName"));
    }

    @FXML
    public void randomSelectGroupAndStudent() {
        // 清空表格中的数据
        studentTable.getItems().clear();

        // 调用服务类获取随机小组
        String group = StudentService.getRandomGroup();

        if (group != null) {
            // 更新选择的小组名
            selectedGroupLabel.setText(group);
        } else {
            System.out.println("没有可用的小组");
        }
    }

    @FXML
    public void drawStudent() {
        // 获取当前选择的小组名
        String group = selectedGroupLabel.getText();

        if (!"未选择".equals(group)) {
            // 从该小组中随机选择一个学生
            Student student = StudentService.getRandomStudentByGroup(group);

            if (student != null) {
                // 清空表格并添加新学生
                ObservableList<Student> students = FXCollections.observableArrayList();
                students.add(student);
                studentTable.setItems(students);
            } else {
                showAlertDialog("提示", "该小组中没有学生");
            }
        } else {
            showAlertDialog("提示", "请选择一个小组");
        }
    }

    @FXML
    public void handleReturn(javafx.event.ActionEvent event) {
        try {
            // 加载主菜单界面的 FXML 文件
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("menu-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 400, 300); // 设置主菜单界面的大小
            Stage stage = (Stage) ((Button) event.getSource()).getScene().getWindow();
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void showAlertDialog(String title, String content) {
        Platform.runLater(() -> {
            // 显示提示对话框
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle(title);
            alert.setHeaderText(null);
            alert.setContentText(content);
            alert.showAndWait();
        });
    }
}
